﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using System.Collections;
using CardLogistics.Model.CardRecovery;

namespace CardLogistics.DAL.CardRecovery
{
    public class CARDRECOVERY_RETURNCARDData
    {
        #region 新增或编辑操作
        /// <summary>
        /// 执行新增或编辑操作
        /// </summary>
        /// <param name="listSQL"></param>
        /// <returns></returns>
        public int AddOrEdit(ArrayList listSQL)
        {
            return DbHelperSQL.ExecuteSqlTran(listSQL);
        }
        #endregion

        #region 重载新增信息
        /// <summary>
        /// 重载新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(CARDRECOVERY_RETURNCARD model, string FileValue)
        {
            string StrOutPar = string.Empty;
            int val = 0;
            string sqlConn = DbHelperSQL.ConnectionString;
            using (OracleConnection conn = new OracleConnection(sqlConn))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;


                StringBuilder strSql = new StringBuilder();
                try
                {
                    string gid = System.Guid.NewGuid().ToString();//主键ID
                    OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
					new OracleParameter("V_RETURNCARDNO", OracleType.VarChar,50),
					new OracleParameter("V_CONID", OracleType.VarChar,50),
					new OracleParameter("V_RETURNDATA", OracleType.DateTime),
					new OracleParameter("V_NOTE", OracleType.NVarChar),
					new OracleParameter("V_ATTN", OracleType.VarChar,50),
					new OracleParameter("V_REMARKS", OracleType.NVarChar),
					new OracleParameter("V_LASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("V_LASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("V_ISSUBMIT", OracleType.Number,4),
                    new OracleParameter("v_ID",OracleType.VarChar,50)};
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Value = model.RETURNCARDNO;
                    parameters[2].Value = model.CONID;
                    parameters[3].Value = model.RETURNDATA;
                    parameters[4].Value = model.NOTE;
                    parameters[5].Value = model.ATTN;
                    parameters[6].Value = model.REMARKS;
                    parameters[7].Value = model.LASTUPDATEUSER;
                    parameters[8].Value = model.LASTUPDATEDATE;
                    parameters[9].Value = model.ISSUBMIT;
                    parameters[10].Value = gid;

                    string[] split = FileValue.Split(';');//以分号分隔
                    int count = split.Length; //计算行数

                    strSql = new StringBuilder(" begin ");
                    for (int k = 0; k < count; k++)
                    {
                        string boxno = split[k].Split('|')[0];
                        string cardno = split[k].Split('|')[1];
                        string startno = split[k].Split('|')[2];
                        string endno = split[k].Split('|')[3];
                        string deservednumber = split[k].Split('|')[4];
                        string actualnumber = split[k].Split('|')[5];
                        string missingcardno = split[k].Split('|')[6];

                        strSql.Append(" insert into cardrecovery_returncard_detail");
                        strSql.Append("(boxno,cardno,startno,endno,deservednumber,actualnumber,missingcardno,returnId)");
                        strSql.Append(" values(");
                        strSql.Append(boxno + "," + cardno + "," + startno + "," + endno + "," + deservednumber + "," + actualnumber + ",");
                        strSql.Append(missingcardno + ",'" + gid + "');");

                        #region 插入卡明细
                        //strSql.Append("P_CARDRECORD_Add(" + startno + "," + endno + ",");
                        //strSql.Append("'0',0,'" + gid + "',to_date('" + model.RETURNDATA.ToString() + "','yyyy-MM-dd HH24:mi:ss'),");
                        //strSql.Append("1," + k + ",0);");
                        #endregion

                    }
                    strSql.Append(" end;");

                    if (!String.IsNullOrEmpty(strSql.ToString()))
                    {
                        cmd.CommandText = strSql.ToString();
                        val += cmd.ExecuteNonQuery();
                    }
                    StrOutPar = DbHelperSQL.RunProcedure("P_CARDRECOVERY_RETURNCARD_ADD", parameters, parameters[0]).ToString();
                    if (StrOutPar.Split(',')[0] == "ok")
                        tx.Commit();
                    else
                        tx.Rollback();
                    return StrOutPar;

                }
                catch (System.Data.OracleClient.OracleException e)
                {

                    tx.Rollback();
                    throw new Exception(e.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }
        #endregion

        #region 重载修改信息
        /// <summary>
        /// 重载修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(CARDRECOVERY_RETURNCARD model, string FileValue)
        {
            string StrOutPar = string.Empty;
            int val = 0;
            string sqlConn = DbHelperSQL.ConnectionString;
            using (OracleConnection conn = new OracleConnection(sqlConn))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    OracleParameter[] parameters = {
                    new OracleParameter("V_ID", OracleType.VarChar,50),
					new OracleParameter("V_RETURNCARDNO", OracleType.VarChar,50),
					new OracleParameter("V_CONID", OracleType.VarChar,50),
					new OracleParameter("V_RETURNDATA", OracleType.DateTime),
					new OracleParameter("V_NOTE", OracleType.NVarChar),
					new OracleParameter("V_ATTN", OracleType.VarChar,50),
					new OracleParameter("V_REMARKS", OracleType.NVarChar),
					new OracleParameter("V_LASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("V_LASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("V_ISSUBMIT", OracleType.Number,4),
                    new OracleParameter("v_varOut", OracleType.VarChar,50)};
                    parameters[0].Value = model.ID;
                    parameters[1].Value = model.RETURNCARDNO;
                    parameters[2].Value = model.CONID;
                    parameters[3].Value = model.RETURNDATA;
                    parameters[4].Value = model.NOTE;
                    parameters[5].Value = model.ATTN;
                    parameters[6].Value = model.REMARKS;
                    parameters[7].Value = model.LASTUPDATEUSER;
                    parameters[8].Value = model.LASTUPDATEDATE;
                    parameters[9].Value = model.ISSUBMIT;
                    parameters[10].Direction = ParameterDirection.Output;

                    string[] split = FileValue.Split(';');//以分号分隔
                    int count = split.Length; //计算行数
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("begin delete from cardrecovery_returncard_detail where returnId ='" + model.ID + "';");
                    for (int k = 0; k < count; k++)
                    {
                        string boxno = split[k].Split('|')[0];
                        string cardno = split[k].Split('|')[1];
                        string startno = split[k].Split('|')[2];
                        string endno = split[k].Split('|')[3];
                        string deservednumber = split[k].Split('|')[4];
                        string actualnumber = split[k].Split('|')[5];
                        string missingcardno = split[k].Split('|')[6];

                        strSql.Append(" insert into cardrecovery_returncard_detail");
                        strSql.Append("(boxno,cardno,startno,endno,deservednumber,actualnumber,missingcardno,returnId)");
                        strSql.Append(" values(");
                        strSql.Append(boxno + "," + cardno + "," + startno + "," + endno + "," + deservednumber + "," + actualnumber + ",");
                        strSql.Append(missingcardno + ",'" + model.ID + "');");
                    }
                    strSql.Append(" end;");

                    if (!String.IsNullOrEmpty(strSql.ToString()))
                    {
                        cmd.CommandText = strSql.ToString();
                        val += cmd.ExecuteNonQuery();
                    }
                    StrOutPar = DbHelperSQL.RunProcedure("P_CARDRECOVERY_RETURNCARD_Upd", parameters, parameters[10]).ToString();
                    if (StrOutPar.Split(',')[0] == "ok")
                    {
                        tx.Commit();
                    }
                    return StrOutPar;
                }
                catch (System.Data.OracleClient.OracleException e)
                {

                    tx.Rollback();
                    throw new Exception(e.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "V_CARDRECOVERY_RETURNCARD", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CARDRECOVERY_RETURNCARD GetModel(string id)
        {
            DataRow row = GetInfoById(id).Rows[0];
            CARDRECOVERY_RETURNCARD model = new CARDRECOVERY_RETURNCARD();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["RETURNCARDNO"] != null && row["RETURNCARDNO"].ToString() != "")
                {
                    model.RETURNCARDNO = row["RETURNCARDNO"].ToString();
                }
                if (row["CONID"] != null && row["CONID"].ToString() != "")
                {
                    model.CONID = row["CONID"].ToString();
                }
                if (row["RETURNDATA"] != null && row["RETURNDATA"].ToString() != "")
                {
                    model.RETURNDATA = DateTime.Parse(row["RETURNDATA"].ToString());
                }
                if (row["NOTE"] != null && row["NOTE"].ToString() != "")
                {
                    model.NOTE = row["NOTE"].ToString();
                }
                if (row["ATTN"] != null && row["ATTN"].ToString() != "")
                {
                    model.ATTN = row["ATTN"].ToString();
                }
                if (row["REMARKS"] != null && row["REMARKS"].ToString() != "")
                {
                    model.REMARKS = row["REMARKS"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null && row["LASTUPDATEUSER"].ToString() != "")
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["ISSUBMIT"] != null && row["ISSUBMIT"].ToString() != "")
                {
                    model.ISSUBMIT = decimal.Parse(row["ISSUBMIT"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string strOutPar = DbHelperSQL.RunProcedure("P_CARDRECOVERY_RETURNCARD_Del", parameters, parameters[0]).ToString();
                return strOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
